package com.fms.data.repository;

import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;

import com.fms.data.file.FileException;
import com.fms.data.file.IFlowSheetOperations;
import com.fms.model.IDecision;
import com.fms.model.ILogger;
import com.fms.model.domain.FlowSheet;

public class FileFlowSheetRepositoryTest extends TestCase
{
	
	
	
	public void testFlowSheetRepoSaveFlowSheet() throws FileException
	{
		Mockery mock = new Mockery();
		
		final FlowSheet sheet = new FlowSheet("Hello.xls");
		final IFlowSheetOperations ops = mock.mock(IFlowSheetOperations.class);
		final ILogger logger = mock.mock(ILogger.class);
		final IDecision decision = mock.mock(IDecision.class);
		
		mock.checking(new Expectations()
		{{
			oneOf(ops).exists(with(equal(sheet))); will(returnValue(false));
			oneOf(ops).createTemplate(with(equal(sheet)));
			oneOf(logger).log(with(equal("Flow sheet was saved at: Hello.xls")));
		}});
		
		FileFlowSheetRepository r = new FileFlowSheetRepository(ops, logger, decision);
		r.save(sheet);
		mock.assertIsSatisfied();
	}
	
	public void testFlowSheetExceptionThrown() throws FileException
	{
		
		Mockery mock = new Mockery();
		
		final FlowSheet sheet = new FlowSheet("Hello.xls");
		final IFlowSheetOperations ops = mock.mock(IFlowSheetOperations.class);
		final ILogger logger = mock.mock(ILogger.class);
		final IDecision decision = mock.mock(IDecision.class);
		
		mock.checking(new Expectations()
		{{
			oneOf(ops).exists(with(equal(sheet))); will(returnValue(false));
			oneOf(ops).createTemplate(with(equal(sheet))); will(throwException(new FileException("Exception text")));
			oneOf(logger).log(with(equal("Could not create the template: Exception text")));
		}});
		
		FileFlowSheetRepository r = new FileFlowSheetRepository(ops, logger, decision);
		r.save(sheet);
		mock.assertIsSatisfied();
		
	}
	
	
	public void testFlowSheetRepoCreateNewFlowSheet() throws FileException
	{
		Mockery mock = new Mockery();
		
		final FlowSheet sheet = new FlowSheet("Hello.xls");
		final IFlowSheetOperations ops = mock.mock(IFlowSheetOperations.class);
		final ILogger logger = mock.mock(ILogger.class);
		final IDecision decision = mock.mock(IDecision.class);
		
		mock.checking(new Expectations()
		{{
			oneOf(ops).exists(with(equal(sheet))); 
						will(returnValue(true));
						
			oneOf(decision).decide(with(equal("File Exists")), with(equal("A file already exists at this location, would you like to continue?")));
						will(returnValue(true));
						
			oneOf(ops).delete(with(equal(sheet.getFileName()))); 
						
			oneOf(ops).createTemplate(with(equal(sheet)));
			oneOf(logger).log(with(equal("Flow sheet was saved at: Hello.xls")));
		}});
		
		FileFlowSheetRepository r = new FileFlowSheetRepository(ops, logger, decision);
		r.saveAsNew(sheet);
		mock.assertIsSatisfied();
	}
	
	public void testFlowSheetRepoSaveSuccessful() throws FileException
	{
		Mockery mock = new Mockery();
		
		final FlowSheet sheet = new FlowSheet("testData/testFlowSheetRepoSaveSuccessful.xls");
		final IFlowSheetOperations ops = mock.mock(IFlowSheetOperations.class);
		final ILogger logger = mock.mock(ILogger.class);
		final IDecision decision = mock.mock(IDecision.class);
		
		mock.checking(new Expectations()
		{{
			oneOf(ops).exists(with(equal(sheet))); 
						will(returnValue(true));
						
			
			//oneOf(ops).save(with(equal(sheet)));
						
			//oneOf(logger).log(with(equal("Flow sheet was saved at: testData/testFlowSheetRepoSaveSuccessful.xls")));
		}});
		
		FileFlowSheetRepository r = new FileFlowSheetRepository(ops, logger, decision);
		r.save(sheet);
		mock.assertIsSatisfied();
	}
}
